package com.example.rabbitmqdemo.routing;

import com.example.rabbitmqdemo.util.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeoutException;

public class Send {
    private static final String EXCHANGE_NAME = "test_exchange_direct";

    public static void main(String[] args) throws IOException, TimeoutException {

        Executors.newCachedThreadPool();
        Executors.newSingleThreadExecutor();
        Executors.newScheduledThreadPool(5);
        Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
        Connection connection = ConnectionUtils.getConnection();
        Channel channel = connection.createChannel();

        //exchange
        channel.exchangeDeclare(EXCHANGE_NAME, "direct");
        String msg = "hello direct !";

        String routingKey = "error";
        channel.basicPublish(EXCHANGE_NAME, routingKey, null, msg.getBytes());

        System.out.println("send: " + msg);

        channel.close();
        connection.close();

        String we_are_happy = replaceSpace("We Are Happy");
        System.out.println(we_are_happy);
        ListNode listNode = new ListNode(67);
        listNode.next = new ListNode(0);
        listNode.next = new ListNode(24);
        listNode.next = new ListNode(58);
        ArrayList<Integer> integers = printListFromTailToHead(listNode);
        System.out.println(integers);
    }

    public static String replaceSpace(String s) {
        // write code here
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == ' ') {
                result.append("%20");
            } else {
                result.append(c);
            }
        }
        return result.toString();
    }

    public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> list = new ArrayList<>(4);
        while (listNode!=null) {
            list.add(listNode.val);
            listNode = listNode.next;
        }
        ArrayList<Integer> result = new ArrayList<>(4);
        for (int i = list.size() - 1; i >= 0; i--) {
            result.add(list.get(i));
        }
        return result;
    }
}

class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
